package com.wbao.dianniu.ui;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 岗位选择
 * 
 * @author yejianjun
 *
 */
public class JobSelectActivity extends BaseActivity {

	private GridView mGridView;
	private ArrayList<Status> dataList = new ArrayList<Status>();
	private String selectedStr = "";
	private Button rightBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_trade_list,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		setTitleName("岗位");
		mGridView = (GridView) findViewById(R.id.trade_list_gridview);
		MyAdapter adapter = new MyAdapter(this);
		Intent intent = getIntent();
		String job = intent.getStringExtra("jobSelect");
		if(TextUtils.isEmpty(job)){
			job = "";
		}
		String[] data_item = getResources().getStringArray(R.array.job_array);
		for (int i = 0; i < data_item.length; i++) {
			Status st = new Status();
			if(data_item[i].equals(job)){
				st.status = true;
				selectedStr = data_item[i];
			}else{
				st.status = false;
			}
			st.value = data_item[i];
			st.index = i;
			dataList.add(st);
		}
		adapter.setData(dataList);
		mGridView.setAdapter(adapter);
		rightBtn = getRightTextButton();
		rightBtn.setText(getResources().getString(R.string.save));
		rightBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			this.finish();
			break;
		case R.id.right_button:	//保存
			if(TextUtils.isEmpty(selectedStr)){
				Notification.toast(this, "选择岗位");
				return;
			}
			Intent intent = getIntent();
			// 把返回数据存入Intent
			intent.putExtra(Const.INTENT_MORE_TRADE, selectedStr);
			// 设置返回数据
			setResult(RESULT_OK, intent);
			this.finish();
			break;
		default:
			break;
		}
		
	}
	
	
	class MyAdapter extends BaseAdapter {

		private ArrayList<Status> mData;
		private Context _context;

		public MyAdapter(Context context) {
			if (null == mData) {
				mData = new ArrayList<Status>();
			}
			_context = context;
		}

		public void setData(ArrayList<Status> data) {
			mData = data;
		}

		private void changedData(int currentId, boolean checked ){
			for (int i = 0; i < mData.size(); i++) {
				if(currentId != i){
					//未选中状态 
					mData.get(i).status = false;
				}else{
					mData.get(i).status = checked;
				}
			}
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (null == convertView) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(_context).inflate(R.layout.authirty_gridview_item, null);
				holder.textview = (CheckedTextView) convertView.findViewById(R.id.gridview_checktext);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textview.setText(mData.get(position).value);
			holder.textview.setChecked(mData.get(position).status);
			holder.textview.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					changedData(position, !v.isSelected());
					if(!v.isSelected()){
						selectedStr = mData.get(position).value;
					}else{
						selectedStr = "";
					}
				}
			});
			return convertView;
		}

		class ViewHolder {
			CheckedTextView textview;
		}
	}
	
	class Status{
		public int index;
		public boolean status;
		public String value;
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
